package fel.via.cz.thewolfofwallstreet.components;

import android.app.Activity;
import android.util.Log;
import android.widget.RelativeLayout;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.GraphViewSeries;
import com.jjoe64.graphview.LineGraphView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

import fel.via.cz.thewolfofwallstreet.LoginActivity;
import fel.via.cz.thewolfofwallstreet.MainActivity;
import fel.via.cz.thewolfofwallstreet.R;

public class ShareGraphView {

    private Activity activity;
    private RelativeLayout graphLayout;
    private String companyName;
    private ShareNewsView shareNewsView;

    public ShareGraphView(Activity activity) {
        this.activity = activity;
        this.graphLayout = (RelativeLayout) activity.findViewById(R.id.Graph);
    }

    public void setup(String companyName, ShareNewsView shareNewsView) {
        this.companyName = companyName;
        this.shareNewsView = shareNewsView;
        httpRequest();
    }

    private void httpRequest() {
        final String url = MainActivity.serverAddress + "wolfofwallstreet/companies";
        final JSONObject object = new JSONObject();
        try {
            object.put("company", companyName);
            object.put("from", "2014-12-08 00:00:00"); // bohzel zatim napevno
            object.put("to", "2014-12-09 00:00:00");
        } catch (JSONException e) {
            Log.e("ShareInfoView ", url);
        }

        JsonObjectRequest postRequest = new JsonObjectRequest(Request.Method.POST, url, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        Log.d("Response", response.toString());
                        JSONArray array = new JSONArray();
                        try {
                            array = response.getJSONArray("history");
                        } catch (JSONException e) {
                            Log.e("ShareGraphView", "chyba parsovani history");
                        }
                        displayData(array);
                        setupNews();
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.e("Error.Response", error.toString());
                    }
                }
        ) {
            @Override
            public HashMap<String, String> getParams() {
                HashMap<String, String> params = new HashMap<String, String>();
                params.put("Accept", "application/json");
                params.put("Content-Type", "application/json");

                return params;
            }

            @Override
            public byte[] getBody() {
                return object.toString().getBytes();
            }
        };
        postRequest.setRetryPolicy(new DefaultRetryPolicy(
                60000,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        LoginActivity.requestQueue.add(postRequest);
    }

    private void setupNews() {
        this.shareNewsView.setup(companyName);
    }

    private void displayData(JSONArray data) {
        // http://android-graphview.org/
        if (data.length() == 0) return;
        GraphViewSeries graph;
        GraphView.GraphViewData[] graphData = new GraphView.GraphViewData[data.length()];
        for (int i = 0; i < data.length(); i++) {
            try {
                JSONObject history = data.getJSONObject(i);
                graphData[i] = new GraphView.GraphViewData(i, history.getDouble("pricePerStock"));
            } catch (JSONException e) {
                Log.e("JSONObject error: ", e.toString());
            }
        }
        graph = new GraphViewSeries(graphData);
        GraphView graphView = new LineGraphView(activity, "Akcie " + companyName);
        graphView.setShowHorizontalLabels(false);
        graphView.addSeries(graph);

        this.graphLayout.removeAllViews();
        this.graphLayout.addView(graphView);
    }
}
